// Java 객체를 JS 객체로 변환
function converterToJS(oObject) {

	// HashMap -> Object
	if (oObject instanceof java.util.HashMap) {

		var oDatas = {};
		var aKeys = oObject['keySet']().toArray();

		for (var nIndex in aKeys) {

			var sKey = aKeys[nIndex];
			var sVal = oObject.get(sKey);

			oDatas[sKey] = converterToJS(sVal);

		}

		return oDatas;

	// ArrayList -> Array
	} else if (oObject instanceof java.util.ArrayList) {

		var aDatas = [];

		for (var i = 0, len = oObject.size(); i < len; i++)
			aDatas.push(converterToJS(oObject.get(i)));

		return aDatas;

	// Double/Float/Integer -> Number
	} else if (oObject instanceof java.lang.Double || oObject instanceof java.lang.Float || oObject instanceof java.lang.Integer) {

		return parseFloat(oObject);

	// String -> String
	} else if (oObject instanceof java.lang.String) {

		return String(oObject);

	}

	return oObject;

}

// JS 객체를 Java 객체로 변환
function converterToJava(oObject) {

	// Array -> ArrayList
	if (oObject.constructor === Array) {

		var oArray = new java.util.ArrayList();

		for (var i = 0, len = oObject.length; i < len; i++)
			oArray.add(converterToJava(oObject[i]));

		return oArray;

	// Object -> HashMap
	} else if (oObject.constructor === Object) {

		var oHash = new java.util.HashMap();

		for (var sKey in oObject)
			oHash.put(sKey, converterToJava(oObject[sKey]));

		return oHash;

	}

	return oObject;

}

/**
 * @author hooriza (ajaxUI 3 team)
 *
 * ArrayList �� ���ڷ� �޾Ƶ��̴� �Լ�,
 * �������� Number
 *
 * ��� ��)
 *
 *  ArrayList foo = new ArrayList();
 *	foo.add(10);
 *	foo.add(20.1);
 *	foo.add(30.5);
 *
 *	invokeMethod_1(foo);
 *
 * ��� ��)
 *
 *	60.6
 */
function invokeMethod_1(oArrayList) {

	var oList = $A(converterToJS(oArrayList));
	var nRet = 0;

	oList.forEach(function(v) {
		nRet += v;
	});

	return converterToJava(nRet);

}

/**
 * @author hooriza (ajaxUI 3 team)
 *
 * HashMap �� ���ڷ� �޾Ƶ��̴� �Լ�,
 * �������� String
 *
 * ��� ��)
 *
 *  HashMap foo = new HashMap<String, String>();
 *	foo.set('alpha', 'one');
 *	foo.set('beta', 'two');
 *	foo.set('gamma', 'three');
 *
 *	invokeMethod_2(foo);
 *
 * ��� ��)
 *
 *	onetwothree
 */
function invokeMethod_2(oHashMap) {

	var oObject = $H(converterToJS(oHashMap));
	var sRet = '';

	oObject.forEach(function(v, k) {
       if(k != 'Request' && k !='Response')
         sRet += v;
	});

	return converterToJava(sRet);

}

/**
 * @author hooriza (ajaxUI 3 team)
 *
 * HashMap �� ArrayList �� ���ڷ� �޾Ƶ��̴� �Լ�,
 * �������� ArrayList
 *
 * ��� ��)
 *
 *	ArrayList foo = new ArrayList<HashMap>();
 *
 *  HashMap bar = new HashMap<String, String>();
 *	bar.set('alpha', 'one');
 *	bar.set('beta', 'two');
 *	bar.set('gamma', 'three');
 *
 *	foo.add(bar);
 *
 *	invokeMethod_3(foo);
 *
 * ��� ��)
 *
 *	[ 'alphabetagamma' ]
 */
function invokeMethod_3(oArrayList) {

	var oList = $A(converterToJS(oArrayList));
	var aRet = [];

	oList.forEach(function(oItem) {

		var sStr = '';

		for (var k in oItem)
			sStr += k;

		aRet.push(sStr);

	});

	return converterToJava(aRet);

}

/**
 * @author hooriza (ajaxUI 3 team)
 *
 * ���ڰ� ������ ������ ���� �����ϴ� �Լ�
 * ���ڰ� ���� ��� HashMap ���� ���ڸ� �޾Ƶ���
 * �������� HashMap
 *
 * ��� �� #1)
 *
 *	invokeMethod_4();
 *
 * ��� �� #1)
 *
 *	{ 'foo' : 'DEFAULT' }
 *
 * ��� �� #2)
 *
 *  HashMap foo = new HashMap<String, Integer>();
 *	foo.set('alpha', 1);
 *	foo.set('beta', 2);
 *	foo.set('gamma', 3);
 *
 *	invokeMethod_4(foo);
 *
 * ��� �� #2)
 *
 *	{
 *		'alpha' : 10,
 *		'beta' : 20,
 *		'gamma' : 30
 *	}
 */
function invokeMethod_4(oHashMap) {

	var oRet;

	if (typeof oHashMap == 'undefined') {

		oRet = { 'foo' : 'DEFAULT' };

	} else {

		var oObject = $H(converterToJS(oHashMap));
		oObject.forEach(function(v, k, o) {
			o[k] *= 10;
		});

		oRet = oObject.$value();

	}

	return converterToJava(oRet);

}

/**
 * @author hooriza (ajaxUI 3 team)
 *
 * HashMap �� ���ڷ� �޾Ƶ��̴� �Լ�,
 * �������� ArrayList
 *
 * ��� �� #2)
 *
 *  HashMap foo = new HashMap<String, Integer>();
 *	foo.set('alpha', 1);
 *	foo.set('beta', 2);
 *	foo.set('gamma', 3);
 *
 *	invokeMethod_5(foo);
 *
 * ��� �� #2)
 *
 *	[ 'alphabetagamma', 60 ]
 */
function invokeMethod_5(oHashMap) {

	var oObject = $H(converterToJS(oHashMap));
	var aRet = [ '', 0 ];

	oObject.forEach(function(v, k) {
		aRet[0] += k;
		aRet[1] += v;
	});

	return converterToJava(aRet);

}

function invokeMethod_6(oArrayList) {

	//var oRet;
	//var oArray = [];

	//if (typeof oHashMap == 'undefined') {

	//	oRet = { 'foo' : 'DEFAULT' }

	//} else {

	//	var oObject = converterToJS(oHashMap);
	//	oRet = oObject;

	//}


	//return converterToJava(oRet);

    //var oArray = [];
	//oArray.push({ name : 'A', address : 'B', phone : 'C', email : 'D'});
	//return converterToJava(oArray);
    return oArrayList;
}

function invokeMethod_7(oHashMap) {

	var oRet;

	if (typeof oHashMap == 'undefined') {

		oRet = { 'foo' : 'DEFAULT' };

	} else {
		var oObject = $H(converterToJS(oHashMap));
		oRet = oObject.$value();
	}

	return converterToJava(oRet);

}